package testUnit;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import FamilyTherapy.MagicToolCard;
import FamilyTherapy.Player;

public class testPlayer {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}


	@Test
	public void testGetRoleNames() {
		//Player p = new Player();
		String result = Player.getRoleNames();
		assertEquals(result,"0.Grandpa 1.Grandma 2.Father 3.Mother 4.Son 5.Daughter ");
	}
	
	@Test
	public void testSetRole_branchInsideFor() throws Exception {
		
		//preparation		
		//change private thing for testing		
		//Field noOfPlayer = p.getClass().getDeclaredField("noOfPlayer");
		//int[] usedRoles=(int[]) Array.newInstance(int.class, 6);
		Player p = Player.class.newInstance();
		Method setRole = p.getClass().getDeclaredMethod("setRole", int.class);
				
		//noOfPlayer.setAccessible(true);
		//noOfPlayer.setInt(p, 1);		
		//Array.set(usedRoles, 0, 0);
		//noOfPlayer.setAccessible(false);	
		setRole.setAccessible(true);	

		//for testing output
		final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		
		String expected = "Repeated role, please select another role: \n";
		
		//for testing input
		String data = "2"; 	//data to be input 
		InputStream stdin = System.in; 
		try {  
			System.setIn(new ByteArrayInputStream(data.getBytes()));	
			setRole.invoke(p, 0);				
		} 
		finally {  
			System.setIn(stdin); 	//reset input stream as System.in
		} 	
		setRole.setAccessible(false);	
				
		assertEquals(expected,outContent.toString());
	}
	
	
	@Test
	public void testSetRole_statement() {		
		Player p =new Player("abc", 1); // set the 1 to setRole already
		int result= p.getRole();
		assertEquals(result,1);
	}
	
	@Test
	public void testPlayer() {
		boolean isNEW=false;
		Player p = new Player();
		
		if(p != null)
			isNEW=true;
		assertEquals(true,isNEW);
	}
	@Test
	public void testGetRole() {
		Player p = new Player("abc",1);
		int result = p.getRole() ;
		assertEquals(result,1);
	}

	@Test
	public void testGetRoleName() {
		Player p = new Player("abc",1);
		String result = p.getRoleName();
		assertEquals(result,"Grandma");
	}

	@Test
	public void testGetName() {
		Player p = new Player("abc",1);
		String result = p.getName();
		assertEquals(result,"abc");
		
	}

	@Test
	public void testGetPoint() {
		Player p = new Player("abc",1);
		int result = p.getPoint();
		assertEquals(result,5);
	}

	@Test
	public void testAddpoint() {
		Player p = new Player("abc",1);
		p.addPoint(3);
		int result = p.getPoint();
		assertEquals(result,8);
	}
	

	@Test
	public void testDeductPoint() {
		Player p = new Player("abc",1);
		p.deductPoint(3);
		int result = p.getPoint();
		assertEquals(result,2);
	}
	

	@Test
	public void testGetPosition() {
		Player p = new Player("abc",1);
		p.setPosition(10);
		int result = p.getPosition();
		assertEquals(result,10);
	}

	

	@Test
	public void testSetPosition() {
		Player p = new Player("abc",1);
		p.setPosition(10);
		int result = p.getPosition();
		assertEquals(result,10);
	}
	
	@Test
	public void testGetHoldingCard() {
		Player p = new Player("abc",1);
		p.addHoldingCard(10);
		ArrayList<Integer> result = p.getHoldingCard();
		int expected = result.get(0);
		assertEquals(expected,10);
	}
	
	@Test
	public void testDeductHoldingCard() {
		Player p = new Player("abc",1);
		p.addHoldingCard(10);
		p.addHoldingCard(20);
		p.deductHoldingCard(20);
		ArrayList<Integer> result = p.getHoldingCard();
		assertEquals(result.isEmpty(),false);
	}

	@Test
	public void testAddHoldingCard() {
		Player p = new Player("abc",1);
		p.addHoldingCard(10);
		ArrayList<Integer> result = p.getHoldingCard();
		int result2 = result.get(0);
		assertEquals(result2,10);
	}

}
